package com.franklin.ideaplugin.easytesting.linemarker;

import cn.hutool.core.collection.CollectionUtil;
import com.franklin.ideaplugin.api.linemarker.AbstractLineMarkerProvider;
import com.franklin.ideaplugin.api.utils.PsiUtil;
import com.franklin.ideaplugin.easytesting.icon.PluginIcons;
import com.franklin.ideaplugin.easytesting.rpc.RegistryCenter;
import com.franklin.ideaplugin.easytesting.rpc.entity.RegistryData;
import com.intellij.codeInsight.daemon.LineMarkerInfo;
import com.intellij.pom.Navigatable;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiJavaFile;
import com.intellij.psi.PsiMethod;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.util.Collections;
import java.util.List;

/**
 * @author Ye Junhui
 * @since 2023/5/12
 */
public class EasyTestingMethodLineMarker extends AbstractLineMarkerProvider<PsiMethod> {
    @Override
    protected boolean isMarkerTarget(@NotNull PsiElement psiElement) {
        if (!(psiElement.getContainingFile() instanceof PsiJavaFile)){
            return false;
        }
        if (psiElement instanceof PsiMethod){
            PsiMethod psiMethod = (PsiMethod) psiElement;
            if (psiMethod.isConstructor()){
                return false;
            }
            if (PsiUtil.isMainMethod(psiMethod)){
                return false;
            }
            List<RegistryData> registryDataList = RegistryCenter.getRegisterServer(psiMethod);
            return CollectionUtil.isNotEmpty(registryDataList);
        }
        return false;
    }

    @Override
    public @NotNull Icon getIcon() {
        return PluginIcons.RUN_ICON;
    }

    @Override
    public LineMarkerInfo<PsiMethod> getLineMarkerInfo(@NotNull PsiElement psiElement) {
        if (!isMarkerTarget(psiElement)){
            return null;
        }
        PsiMethod psiMethod = getObject(psiElement);
        return new SelectMethodLineMarkerInfo(
                psiMethod,
                getIcon(),
                (Navigatable) psiMethod.getNavigationElement(),
                Collections.singleton("")
        );
    }
}
